********************************************************************************
* timeseries of funding premia for BTC 
********************************************************************************

use "build/output/futures_funding_long.dta", clear
duplicates drop

keep if exchange == "Binance"

keep if inlist(coin, "BTC", "ETH")

gen pair = coin+base
keep funding_apr date pair

reshape wide funding_apr, i(date) j(pair) string

tsset date

tssmooth ma funding_aprBTCUSDT_7d=funding_aprBTCUSDT, window(6 1 0)
tssmooth ma funding_aprETHUSDT_7d=funding_aprETHUSDT, window(6 1 0)

label var funding_aprBTCUSDT_7d "BTC/USDT"
label var funding_aprETHUSDT_7d "ETH/USDT"
format date %tdm-CY

tsline funding_aprBTCUSDT_7d funding_aprETHUSDT_7d, subtitle("Percent (annualized)",  orientation(horizontal)  bexpand justification(left)) plotregion(margin(medium)) ytitle("") xtitle("") scale(*1.2)  legend(ring(0) position(2) rows(2)) 


gr export "analysis/output_figures/figure_btc_perp_premium.pdf", replace
